package com.cskaoyan.market.controller.wx;


import com.cskaoyan.market.annotation.FlashList;
import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxFootPrintService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxFootPrintListbriefVo;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("wx/footprint")
public class WxFootPrintController {
    @Autowired

    SecurityManager securityManager;
    @Autowired
    WxFootPrintService wxFootPrintService;
    @FlashList(runningType = "before",method = "footPrint")
    @GetMapping("list")
    public Object list() {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        Integer userId = user.getId();

        List<WxFootPrintListbriefVo> resultListVo = wxFootPrintService.getFootprintsByUserId(userId);
        PageInfo<WxFootPrintListbriefVo> marketGoodsPageInfo = new PageInfo<>(resultListVo);
        resultListVo.sort((o1, o2) -> o2.getAddTime().compareTo(o1.getAddTime()));
        HashMap<String, Object> data = new HashMap<>();
        data.put("list", marketGoodsPageInfo.getList());
        data.put("total", marketGoodsPageInfo.getTotal());
        data.put("pages", marketGoodsPageInfo.getPages());
        data.put("page", marketGoodsPageInfo.getPageNum());
        data.put("limit", marketGoodsPageInfo.getPageSize());

        return ResponseUtil.ok(data);
    }


    @PostMapping("delete")
    public Object delete(@RequestBody MarketFootprint marketFootprint) {

        return wxFootPrintService.delete(marketFootprint);
    }


}
